package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperWithWebDriverBase {

	protected final ApplicationManager manager;
	protected WebDriver driver;
	
	public HelperWithWebDriverBase(ApplicationManager manager) {
		this.manager = manager;
		driver = manager.getWebDriverHelper().getDriver();
	}
	
	protected void type(By locator, String name) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(name);
	}

	protected WebElement findElement(By locator) {
		return driver.findElement(locator);
	}
	
	protected List<WebElement> findElements (By linkText) {
		return driver.findElements(linkText);
	}
	
	protected void select(By locator, String optionText) {
	    new Select(driver.findElement(locator)).selectByVisibleText(optionText);
	}
	
	protected void selectRandom(By locator) {
	    Select select = new Select(driver.findElement(locator));
	    List<WebElement> options = select.getOptions();
	    int index = new Random().nextInt(options.size());
	    options.get(index).click();
	}
	
	protected void openUrl(String urlString) {
		driver.get(urlString);
	}

	protected void click(By locator) {
		findElement(locator).click();
	}
	
}
